﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Studio77.PopBall.Core;
using Studio77.PopBall.Utils;
using OpenXLive;
using Microsoft.Phone.Tasks;

namespace Studio77.PopBall
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            isOnGift = false;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);


            //清除页面堆栈
            try
            {
                for (int i = 0; i < 4; i++)
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            catch
            { }

            //动画
            StoryboardBall1.RepeatBehavior = RepeatBehavior.Forever;
            StoryboardBall1.Begin();

            StoryboardBall2.RepeatBehavior = RepeatBehavior.Forever;
            StoryboardBall2.Begin();

            StoryboardBall3.RepeatBehavior = RepeatBehavior.Forever;
            StoryboardBall3.Begin();

            StoryboardBall4.RepeatBehavior = RepeatBehavior.Forever;
            StoryboardBall4.Begin();

            StoryboardBall5.RepeatBehavior = RepeatBehavior.Forever;
            StoryboardBall5.Begin();

            this.txtHighScore.Text = StageManager.Instance.HighScore.ToString();

            //继续游戏判断逻辑
            if (StageManager.Instance.SaveData != null)
            {
                this.btnContinue.IsEnabled = true;
                this.txtContinueInfo.Visibility = System.Windows.Visibility.Visible;
                this.txtContinueInfo.Text = "STAGE-" + StageManager.Instance.SaveData.StageIndex;
            }
            else
            {
                this.btnContinue.IsEnabled = false;
                this.txtContinueInfo.Visibility = System.Windows.Visibility.Collapsed;
            }

            if (StageManager.Instance.IsPlaySound)
            {
                this.btnSoundOn.Visibility = System.Windows.Visibility.Visible;
                this.btnSoundOff.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
                this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
            }


            this.btnGift.Visibility = System.Windows.Visibility.Collapsed;
            //if (StageManager.Instance.IsDoNewUserTask)
            //{
            //    this.btnGift.Visibility = System.Windows.Visibility.Collapsed;
            //}
            //else
            //{
            //    this.btnGift.Visibility = System.Windows.Visibility.Visible;
            //}

            //播放背景音乐
            AudioPlayHelper.PlayBackground();
        }

        private void Button_Click_1(object sender, System.Windows.RoutedEventArgs e)
        {
            bool isStart = true;
            if (StageManager.Instance.SaveData != null)
            {
                if (MessageBox.Show("Are you sure to start a new game? The last game record will be lost.", "Tip", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                {
                    isStart = false;
                }
            }
            if (isStart)
            {
                SoundManager.PlayEffect(SoundManager.buttonClick);
                StageManager.Instance.LaunchCount++;
                // 在此处添加事件处理程序实现。
                this.NavigationService.Navigate(new Uri("/Pages/GamePage.xaml?IsNewGame=1", UriKind.RelativeOrAbsolute));
            }
        }

        private void btnContinue_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            StageManager.Instance.LaunchCount++;
            // 在此处添加事件处理程序实现。
            this.NavigationService.Navigate(new Uri("/Pages/GamePage.xaml?IsNewGame=0", UriKind.RelativeOrAbsolute));
        }

        private void btnOpenxlive_Click_1(object sender, RoutedEventArgs e)
        {
            SoundManager.PlayEffect(SoundManager.buttonClick);
            if (XLiveGameManager.CurrentSession != null && XLiveGameManager.CurrentSession.IsValid)
            {
                OpenXLive.Silverlight.XLiveUIManager.ShowLeaderboard();
            }
        }

        private void btnSoundOn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //关闭音效
            StageManager.Instance.IsPlaySound = false;
            AudioPlayHelper.StopBackground();
            this.btnSoundOn.Visibility = System.Windows.Visibility.Collapsed;
            this.btnSoundOff.Visibility = System.Windows.Visibility.Visible;
        }

        private void btnSoundOff_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //打开音效
            StageManager.Instance.IsPlaySound = true;
            AudioPlayHelper.PlayBackground();
            this.btnSoundOn.Visibility = System.Windows.Visibility.Visible;
            this.btnSoundOff.Visibility = System.Windows.Visibility.Collapsed;
        }

        bool isOnGift = false;

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (isOnGift)
            {
                this.StoryboardHidePackage.Begin();
                isOnGift = false;
                e.Cancel = true;
            }
            base.OnBackKeyPress(e);
        }

        private void btnGift_Click_1(object sender, RoutedEventArgs e)
        {
            isOnGift = true;
            this.StoryboardShowPackage.Begin();
        }

        private void btnGetMB_Click_1(object sender, RoutedEventArgs e)
        {
            MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask();
            marketplaceReviewTask.Show();
            StageManager.Instance.IsCommented = true;
            StageManager.Instance.IsDoNewUserTask = true;

            //增加30个魔法球
            StageManager.Instance.MagicBallCount += 30;

            this.btnGetMB.Content = "Already Get";
            this.btnGetMB.IsEnabled = false;
            this.btnGift.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void btnCloseGift_Click_1(object sender, RoutedEventArgs e)
        {
            isOnGift = false;
            this.StoryboardHidePackage.Begin();
        }
    }
}